
/*   RolGPS Project
 * 
 *   Copyright (C) 2009 Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package org.rolgps.libnlink.connection;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rolgps.libnstack.parser.ParserFactory;
import org.rolgps.libnstack.protocol.Message;
import org.rolgps.libnstack.protocol.MessageParser;

/**
 *
 * @author xeph
 */
class SocketSender implements Runnable {

    Socket socket = null;
    boolean shutdown = true;
    MessageParser messageParser = ParserFactory.getDefaultParser();
    BlockingQueue<Message> messagesToSend = new LinkedBlockingQueue<Message>();
    String name = "noname";
    /**
     *
     * @param socket
     */
    public void setSocket(Socket socket, String name) {
        shutdown=false;
        this.socket = socket;
        this.name=name;
    }

    /**
     *
     */
    public void shutdown() {
        shutdown = true;
    }

    /**
     *
     * @param message
     */
    public synchronized void sendMessage(Message message) {
        try {
            messagesToSend.put(message);
        } catch (InterruptedException ex) {
            Logger.getLogger(SocketSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void run() {
        if (socket != null) {
            try {
                Message message = null;
                PrintWriter output = null;
                output = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
                while (!shutdown) {
                    message = messagesToSend.take();
                    message.from(name);
                    String toSend = messageParser.parseMessage(message);
                    output.println(toSend);
                    output.println();
                    output.println();
                    output.flush();

                }
                output.close();
            } catch (InterruptedException ex) {
                Logger.getLogger(SocketSender.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SocketSender.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
